package cn.edu.henu.team.ui;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.snackbar.Snackbar;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import cn.bmob.v3.BmobBatch;
import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BatchResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.adapter.MessageBoardAdapter;
import cn.edu.henu.team.base.BaseActivity;
import cn.edu.henu.team.bean._User;
import cn.edu.henu.team.bean.messageBoard;
import cn.edu.henu.team.utils.MyListview;

public class MessageBoardActivity extends BaseActivity {
    private ImageView back;
    private TextView deleteAll;
    private MyListview lv;
    private MessageBoardAdapter mMessageBoardAdapter;
    private SmartRefreshLayout refreshSrl;
    private ScrollView scrollView;
    private List<messageBoard> list = new ArrayList<messageBoard>();;
    private _User user = BmobUser.getCurrentUser(_User.class);
    private DisplayImageOptions options; // 设置图片显示相关参数

    @Override
    protected int initLayout() {
        return R.layout.activity_message_board;
    }

    @Override
    protected void initView() {
        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        refreshSrl=findViewById(R.id.refresh_srl);
        refreshSrl.setRefreshHeader(new ClassicsHeader(getApplicationContext()));
        refreshSrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshSrl.finishRefresh(1000/*,false*/);
                list.clear();
                initData();
            }
        });

        lv = findViewById(R.id.lv);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                intentToComm(i);
            }
        });

        deleteAll = findViewById(R.id.deleteAll);
        deleteAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<BmobObject> categories = new ArrayList<>();
                for(int i=0;i<list.size();i++){
                    messageBoard m = new messageBoard();
                    m.setObjectId(list.get(i).getObjectId());
                    categories.add(m);
                }
               new BmobBatch().deleteBatch(categories).doBatch(new QueryListListener<BatchResult>() {
                   @Override
                   public void done(List<BatchResult> list, BmobException e) {
                       if (e == null) {
                           boolean flag = false;
                           for (int i = 0; i < list.size(); i++) {
                               BatchResult result = list.get(i);
                               BmobException ex = result.getError();
                               if (ex == null) {
                                  flag = true;
                               }
                           }
                           if(flag){
                               Toast.makeText(MessageBoardActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                           }
                       } else {
                       }
                   }
               });
            }
        });

        // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.upload) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.upload) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.upload) // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true) // 设置下载的图片是否缓存在SD卡中
                .build(); // 构建完成

        scrollView = findViewById(R.id.scroll);
        if (scrollView != null) {
            scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
                @Override
                public void onScrollChanged() {
                }
            });
        }
    }

    @Override
    protected void initData() {
        mMessageBoardAdapter = new MessageBoardAdapter(MessageBoardActivity.this, list);
        lv.setAdapter(mMessageBoardAdapter);

        BmobQuery<messageBoard> query = new BmobQuery<messageBoard>();
        query.addWhereEqualTo("acceptorID",user.getObjectId());
        query.order("-createdAt");
        query.findObjects(new FindListener<messageBoard>() {
            @Override
            public void done(List<messageBoard> lists, BmobException e) {
                if (e == null) {
                    list = lists;
                    mMessageBoardAdapter.addMessageBoard(list);
                    mMessageBoardAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    /*
    传递数据到详情页
   */
    public void intentToComm(int i){
        //传递数据到评论页面
        Intent intent = new Intent(MessageBoardActivity.this, UserHomepageActivity.class);
        intent.putExtra("objectId",list.get(i).getInitiatorID());
        startActivity(intent);
    }
}